import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MyAddFrame extends JFrame {
    private JTextField text1, text2, text3;
    private JButton equal;
    public JDialog alarm;
    private static String[] sign = {"+", "-", "*", "/", "%"};
    private JRadioButton[] signs;

    public MyAddFrame() {
        super("整数算术运算器");
        this.setBounds(600, 500, 800, 100);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);                        //退出程序，结束运行
        this.setLayout(new FlowLayout());
        this.add(text1 = new JTextField(10));
        JPanel allSign = new JPanel(new GridLayout(1, 5));
        this.add(allSign);
        ButtonGroup allSignGroup = new ButtonGroup();
        this.signs = new JRadioButton[sign.length];
        for (int i = 0; i < this.sign.length; i++) {
            allSign.add(this.signs[i] = new JRadioButton(sign[i]));
            allSignGroup.add(this.signs[i]);
        }
        this.add(text2 = new JTextField(10));
        this.add(equal = new JButton("="));
        this.add(text3 = new JTextField(15));
        this.text3.setEditable(false);
        this.setVisible(true);
        this.pack();
        ActionListener mylistener = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                double a = Double.valueOf(text1.getText()), c = 0.0;
                double b = Double.valueOf(text2.getText());

                if (signs[3].isSelected() && b == 0 || signs[4].isSelected() && b == 0)
                    JOptionPane.showMessageDialog(alarm, "数值格式异常，请重新输入！");
                try {
                    if (signs[0].isSelected()) c = a + b;
                    else if (signs[1].isSelected()) c = a - b;
                    else if (signs[2].isSelected()) c = a * b;
                    else if (signs[3].isSelected()) c = a / b;
                    else c = a % b;
                } catch (NumberFormatException ex) {
                }
                text3.setText("" + c);
            }
        };
        this.equal.addActionListener(mylistener);
    }

    public static void main(String[] args) {
        new MyAddFrame();
    }
}
